package org.record.mood.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @description: 日期工具类
 * @author: sunyw
 * @time: 2020-03-12 16:55
 */
public class DateUtils {
    /**
     * 时间戳转换为日期
     *
     * @param  unix
     * @return String
     */
    public static String unixToDate(String unix) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long time = new Long(unix+"000");
        return format.format(time);
    }

    /**
     * 获取当前日期信息
     *
     * @return
     */
    public static  String getYYYYMMDD() {
        SimpleDateFormat sdf = getSimpleDateFormat("yyyy-MM-dd");
        return sdf.format(Calendar.getInstance().getTime());
    }

    /**
     * 日期格式化
     *
     * @param pattern
     * @return
     */
    private static SimpleDateFormat getSimpleDateFormat(String pattern) {
        return new SimpleDateFormat(pattern);
    }

    /**
     * 计算两个时间差
     * @param date
     * @return
     */
    public static String days(String date)  {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Long days = null;
        Date currentTime = null;//现在系统当前时间
        try {
            currentTime = dateFormat.parse(dateFormat.format(new Date()));
            Date pastTime = dateFormat.parse(date);//过去时间
            long diff = currentTime.getTime() - pastTime.getTime();
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return String.valueOf(days);
    }

}
